<?php

namespace app\bh\controller;

use app\bh\model\DataModel;
use app\bh\model\FailepModel;
use app\bh\validate\FailePointValidate;
use think\Controller;
use think\Request;

class FailepController extends BaseAdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        //
        $data = $request->get();
//        $index = array_key_exists('index', $data) ? $data['index'] : 1;
//        $size = array_key_exists('size', $data) ? $data['size'] : 20;
//
        $FailP = new FailepModel();
        if (self::$user['roles'] == 0) {

//        $data = $FailP->limit(($index - 1) * $size, $size)->select();
            $data = $FailP->with(data)->select();

        } else {
            //属于自己的data
            $data = $FailP->with(data)->where('uid', self::$user['id'])->select()->toArray();
            //空gdid
            $kdata = $FailP->where('gdid', '')->select()->toArray();
            foreach ($kdata as $v) {
                array_push($data, $v);
            }

        }
        foreach ($data as $v) {
            $v['gd'] = '';
            if ($v->gdid != '') $v['gd'] = \app\bh\model\DataModel::where('gdid', $v->gdid)->find();
        }

        return $this->SuccessReturn('ok', $data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = $request->post();
        $faileP_validate = new FailePointValidate();
        $result = $faileP_validate->scene('save')->check($data);
        if (!$result) return $this->ErrorReturn(403, $faileP_validate->getError());
        if (!is_array($data['latlng'])) {
            return $this->ErrorReturn(400, '经纬度不是数组类型');
        }

        //验证经纬度
        $lng = (float)$data['latlng'][0];
        $lat = (float)$data['latlng'][1];
        if (abs($lng) > 180 || abs($lat) > 90) return $this->ErrorReturn(400, '经纬度格式不对,经度在前 纬度在后');

        //保存数据
        $failep = new FailepModel();
        $failep->name = $data['name'];
        $failep->latlng = $data['latlng'];
        $failep->distance = $data['distance'];
        $failep->gdid = $data['gdid'];
        $failep->config = $data['config'];
        $failep->uid = self::$user['id'];
//        if (array_key_exists('time', $data)) $failep->create_time = $data['time'];
        if (array_key_exists('time', $data)) {

            $failep->create_time = $data['time'];
        }
        $res = $failep->save();
        $failep->url = '/Vmap/' . $failep->id;

        if ($res) return $this->SuccessReturn('添加成功', $failep);
        return $this->ErrorReturn(500, '添加失败 请重新添加');
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //

        //
        $data = $request->put();
        $faileP_validate = new FailePointValidate();
        $result = $faileP_validate->scene('update')->check($data);
        if (!$result) return $this->ErrorReturn(403, $faileP_validate->getError());
        //获取对象
        $failep = FailepModel::find($data['id']);
        if (!$failep) return $this->ErrorReturn(400, '找不到对应的数据');
        if (key_exists('latlng', $data)) {
            //验证经纬度
            $lng = $data['latlng'][0];
            $lat = $data['latlng'][1];
            if ($lng < -180 || $lng > 180 || $lat < -90 || $lat > 90) {
                return $this->ErrorReturn(400, '经纬度格式不对,经度在前 纬度在后');
            }
            $failep->latlng = $data['latlng'];
        }

        //保存数据

        if (key_exists('name', $data)) $failep->name = $data['name'];
        if (key_exists('iconid', $data)) $failep->iconid = $data['iconid'];
        if (key_exists('distance', $data)) $failep->distance = $data['distance'];
        if (key_exists('gdid', $data)) $failep->gdid = $data['gdid'];
        $res = $failep->save();
        if ($res) return $this->SuccessReturn('修改成功', $failep);
        return $this->ErrorReturn(500, '修改失败 请重新修改');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id, Request $request)
    {
        //
        //
        $id = $request->post('id');
        if ($id == 0) return $this->ErrorReturn(400, '故障点id无效');
        //查询是否存在此用户
        $data = FailepModel::where('id', $id)->find();
        if (!$data) return $this->ErrorReturn(404, '找不到此故障点');
        $data->delete();
        return $this->SuccessReturn('删除成功');
    }
}
